using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

namespace DD.Data.SQL
{
    public static class RatingManager
    {
        /// <summary>
        /// Users rating for product.  Returns 0 if not rated.
        /// </summary>
        /// <param name="uid">User Id</param>
        /// <param name="pid">Product Id</param>
        /// <returns>Rating</returns>
        public static int GetUserProductRating(int uid, int pid)
        {
            SqlConnection conn = new SqlConnection();
            conn = DD.Data.SQL.DataManager.GetConnection();

            SqlCommand com = new SqlCommand("sp_DD_GET_USER_PRODUCT_RATING", conn);
            com.CommandType = CommandType.StoredProcedure;

            SqlParameter param1 = new SqlParameter("@PID", pid);
            param1.SqlDbType = SqlDbType.Int;

            SqlParameter param2 = new SqlParameter("@UID", uid);
            param2.SqlDbType = SqlDbType.Int;

            SqlParameter param3 = new SqlParameter("@RATING", SqlDbType.Int);
            param3.Direction = ParameterDirection.Output;

            com.Parameters.Add(param1);
            com.Parameters.Add(param2);
            com.Parameters.Add(param3);

            int rating = 0;
            try
            {
                conn.Open();
                com.ExecuteNonQuery();
                rating = Convert.ToInt32(param3.Value);
                conn.Close();
            }
            catch
            { }


            return rating;
        }

        public static void AddUpdateUserProductRating(int uid, int pid, int rating)
        {
            SqlConnection conn = new SqlConnection();
            conn = DD.Data.SQL.DataManager.GetConnection();

            SqlCommand com = new SqlCommand("sp_DD_ADD_USER_RATING", conn);
            com.CommandType = CommandType.StoredProcedure;

            SqlParameter param1 = new SqlParameter("@PID", pid);
            param1.SqlDbType = SqlDbType.Int;

            SqlParameter param2 = new SqlParameter("@UID", uid);
            param2.SqlDbType = SqlDbType.Int;

            SqlParameter param3 = new SqlParameter("@RATING", rating);
            param3.SqlDbType = SqlDbType.Int;

            com.Parameters.Add(param1);
            com.Parameters.Add(param2);
            com.Parameters.Add(param3);

            conn.Open();
            com.ExecuteNonQuery();
            conn.Close();
        }
    }
}
